package com.cmcc.aregister.ui.activity;

import org.json.JSONException;
import org.json.JSONObject;
import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class FeedbackActivity extends ParentActivity implements OnClickListener{
	Button feedbackBtn;
	EditText feedbackEdit;
	TextView mTextNum;
	private boolean canSend = true;
	public static final int WEIBO_MAX_LENGTH = 140;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback_layout);
		((TextView) findViewById(R.id.title)).setText(R.string.user_feedback);
		feedbackBtn = (Button) findViewById(R.id.feedback_sub_btn);
		feedbackBtn.setOnClickListener(this);
		feedbackEdit = (EditText) findViewById(R.id.feedback_edit);
		mTextNum = (TextView) findViewById(R.id.tv_text_limit);
		feedbackEdit.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable s) {
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				String mText = feedbackEdit.getText().toString();
				int len = mText.length();
				if (len <= WEIBO_MAX_LENGTH) {
					canSend = true;
					len = WEIBO_MAX_LENGTH - len;
					mTextNum.setTextColor(R.color.text_num_gray);
					if (!feedbackBtn.isEnabled()) feedbackBtn.setEnabled(true);
				}
				else {
					len = len - WEIBO_MAX_LENGTH;

					mTextNum.setTextColor(Color.RED);
					canSend = false;
//					if (mSend.isEnabled()) mSend.setEnabled(false);
				}
				mTextNum.setText(String.valueOf(len));
			}
		});
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == feedbackBtn){
			if(feedbackEdit.getText().toString().equals("")){
				showSingleAlertDlg(getString(R.string.dialog_warning), getString(R.string.warning_feedback_null), getString(R.string.dialog_close),false);
				return;
			}
			if(canSend){
				String msg = feedbackEdit.getText().toString();
				url = ConfigUtilHelper.sendFeedbackMsgUrl + 
						"do=Status.addReply&client_id=90023&client_auth=ade9fbbb41faf7a4ea0fae9912918994&type=501&unid=123456789&token=123456789&content=title&reply_content="
						+ msg;
				sending();
			}else{
				showSingleAlertDlg(getString(R.string.dialog_warning), getString(R.string.dialog_textnumber_too_more), getString(R.string.dialog_close),false);
			}
		}
	}
	
	private void sending(){
		showProgressDlg(getString(R.string.notice),
				getString(R.string.dialog_sending));
	}
	
	
	private void showProgressDlg(String title, String msg) {
		final ProgressDialog loadingDlg = new ProgressDialog(this);
		loadingDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		loadingDlg.setTitle(title);
		loadingDlg.setMessage(msg);
		loadingDlg.setIcon(android.R.drawable.ic_dialog_info);
		loadingDlg.setCancelable(true);
		loadingDlg.show();
		new Thread() {
			public void run() {
				try {
					progressRun();
				} catch (Exception e) {
					loadingDlg.dismiss();
				}
				loadingDlg.dismiss();
			}
		}.start();
		loadingDlg
				.setOnDismissListener(new DialogInterface.OnDismissListener() {

					@Override
					public void onDismiss(DialogInterface dialog) {
						// TODO Auto-generated method stub
						doWork();
					}

				});
	}
	private String result;
	private String url = "";
	private void progressRun() {
		result = ConfigUtilHelper.request(url);
	}
	
	private void doWork() {
		try {
			if(result != null && !result.equals("")){
				JSONObject jb = new JSONObject(result);
				if(jb.has("status") && jb.getString("status").equals("1")){
					showSingleAlertDlg(getString(R.string.notice),
							getString(R.string.dlg_send_success),
							getString(R.string.dialog_yes),true);
					closeSoftKeys(FeedbackActivity.this);
					return;
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!connectedNetwork()){
			showSingleAlertDlg(getString(R.string.notice),
					getString(R.string.dialog_check_internet),
					getString(R.string.dialog_yes));
			return;
		}
		showSingleAlertDlg(getString(R.string.notice),
				getString(R.string.dlg_send_failed),
				getString(R.string.dialog_yes),false);
	}
}
